package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.entity.HouseHistory;
import com.yjps.house.service.HouseService;
import com.yjps.house.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/house")
public class RecommendController {
    @Autowired
    HouseService houseService;
    @Autowired
    RecommendService recommendService;

    /**
     * 经纪人推荐房源（未确认）
     *
     * @param json { "recommend": { "agentId": "1" ,"houseId":""}}
     * @return json 成功返回{data:{"history_id":""}}
     */
    @RequestMapping(value = "/recommend.do", method = RequestMethod.POST)
    @ResponseBody
    public String recommendByAgents(@RequestParam(value = "recommend") String json) {
        //解析json
        JSONObject object = JSON.parseObject(json);
        JSONObject recommend = JSON.parseObject(object.getString("recommend"));
        int agentsId =  Integer.parseInt(recommend.getString("agentsId"));
        int houseId = Integer.parseInt(recommend.getString("houseId"));
        String time = TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);

        //调用业务
        int historyId = recommendService.recommendHouse(houseId,agentsId,time);

        //封装返回
        Message message;
        if (historyId>0){
            message=new Message(true,"推荐房源操作成功");
        }else {
            message=new Message(false,"推荐房源操作失败");
        }
        return message.toJson();
    }


    /**
     * 门店经理获取该门店下经纪人的推荐记录（未确认）
     * @param json {"shopId":"","page":"","pageSize":"","queryType":"unRecommend"}
     * @return 成功返回{data:{publishHistory:{...}...}}
     */
    @RequestMapping(value = "/recommend/shop/list.do",method = RequestMethod.POST )
    @ResponseBody
    public String shopList(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object = JSON.parseObject(json);
        int shopId=Integer.parseInt(object.getString("shopId"));
        int pageSize=Integer.parseInt(object.getString("pageSize"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*pageSize;
        String queryType=object.getString("queryType");

        //调用业务
        boolean flag=true;
        List<HouseHistory> histories =new ArrayList<>();
        if("all".equals(queryType)){
            histories =recommendService.allListByShopId(shopId, startRow, pageSize);
        }else if ("recommend".equals(queryType)){
            //
        }else if ("unRecommend".equals(queryType)){
            histories =recommendService.unRecommendListByShopId(shopId, startRow, pageSize);
        }else{
            //参数错误
            flag=false;
        }

        //封装返回
        Message message;
        if (!histories.isEmpty()){
            message=new Message(true,"", histories);
        }else if(flag){
            message=new Message(true,"");
        }else{
            message=new Message(false,"");
        }
        return message.toJson();
    }
}
